package com.gf.forcesystem.mq;

import com.gf.forcesystem.config.CotaskingRabbitmqConfig;
import com.gf.forcesystem.model.dto.CotaskingTargetState;
import com.gf.forcesystem.api.CotaskingTargetDestroyService;
import com.gf.framework.common.json.GfJsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gaofei
 * @date 2022/6/19 15:42
 * 目标击毁帧数据接收
 */
@Component
@Slf4j
public class TargetDestroyMqReciver {
    @Resource
    List<CotaskingTargetDestroyService> cotaskingTargetDestroyServices;

    @RabbitListener(queues = CotaskingRabbitmqConfig.targetDestroyQueue)
    @RabbitHandler
    public void recv(String data) {
        CotaskingTargetState cotaskingTargetState = GfJsonMapper.string2Obj(data, CotaskingTargetState.class);
        if (cotaskingTargetState == null) {
            log.warn("TargetDestroyMqReciver 收到无法解析的数据: {}", data);
            return;
        }
        log.info("收到无人机目标状态数据: {}", cotaskingTargetState);
        if (CollectionUtils.isEmpty(cotaskingTargetDestroyServices)) {
            return;
        }

        for (CotaskingTargetDestroyService cotaskingTargetDestroyService : cotaskingTargetDestroyServices) {
            try {
                cotaskingTargetDestroyService.deal(cotaskingTargetState);
            } catch (Exception e) {
                log.error("{} 模型击毁帧处理出错", cotaskingTargetDestroyService.getClass().getName(), e);
            }
        }
    }
}
